package com.example.demo.controller.AuthorControl;

import cn.hutool.json.JSONObject;
import com.example.demo.Exception.BackErrorException;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.SendSmsJson;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.tools.RedisUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zhenzi.sms.ZhenziSmsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author: 杨丽辉
 * @Description:
 * @Date:Created in 17:01 2021/2/24
 **/

@RestController
@RequestMapping("/author/userVerify")
@Api(value = "作者验证接口", tags = "作者验证接口")
public class ACodeController {
    @Autowired
    private RedisUtil redisUtil;

    private String apiUrl="https://sms_developer.zhenzikj.com";
    private String appId = "108165";
    private String appSecret = "2a6a26b6-4e8b-4f61-837b-542e521f1f70";

    @ApiOperation(value = "发送短信验证码", notes = "")
    @ApiOperationSupport(author = "杨")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功")
    )
    @PostMapping("/applyVerifyCode")
    public BackResponseEntity<Object> getCode(@RequestBody SendSmsJson sendSmsJson) throws Exception {
        if(ObjectUtils.isEmpty(sendSmsJson.getAccount())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006,"参数缺失");
        }

        JSONObject json;
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        String code = String.valueOf(new Random().nextInt(899999) + 100000);
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("number", sendSmsJson.getAccount());
        params.put("templateId", "3748");
        //这个参数就是短信模板上那两个参数

        String[] templateParams = new String[2];
        templateParams[0] = code;
        templateParams[1] = "2分钟";

        params.put("templateParams", templateParams);
        String result = client.send(params);

        json = new JSONObject(result);

        if(json.getInt("code")==0) {
            json = new JSONObject();
            json.append("memPhone",sendSmsJson.getAccount());
            json.append("code",code);
            json.append("createTime",System.currentTimeMillis());

            redisUtil.set(sendSmsJson.getAccount()  + "SMS:authors", code, 60 * 2);
                return BackResponseEntity.success(ErrCodeEnum.unknown_0,"验证码发送成功");
        }else{
            throw new BackErrorException(ErrCodeEnum.unknown_1096);
        }
    }

    @ApiOperation(value = "校验短信验证码", notes = "")
    @ApiOperationSupport(author = "杨")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功")
    )
    @GetMapping("/checkVerifyCode")
    public BackResponseEntity<Object> checkVerifyCodeAction(String verCode, SendSmsJson sendSmsJson) {
        if (sendSmsJson.getAccount() == null || ObjectUtils.isEmpty(verCode)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "抱歉,参数缺失!");
        }
        //校验验证码
        Object verifyDataObject = redisUtil.get(sendSmsJson.getAccount() + "SMS:authors");
        if (!ObjectUtils.isEmpty(verifyDataObject)) {
            String verifyData = (String) verifyDataObject;
            if (!verCode.equals(verifyData)) {
                return BackResponseEntity.success(ErrCodeEnum.unknown_1001, "抱歉，验证码错误!");
            }
        } else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1000, "抱歉，验证码失效!");
        }
        return BackResponseEntity.success(ErrCodeEnum.unknown_0, "验证通过");
    }

}


